package cn.strategy.decline;

import cn.math.MyStatistics;
import cn.stock.util.MyUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

class Item {
    Double value = 0D;
    int index = -1;
}

/**
 * 查询最近 5 天各板块跌幅最大的股票，且最近两天没有怎么跌的
 * */
public class StockDecline_1 {
    /**
     * 返回板块名列表
     * */
    public static void getTypes() {
        //Hashtable<String, List<List<Object>>> ht = new Hashtable<>();
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            conn = MyUtil.getConnection();
            stmt = conn.prepareStatement("SELECT NAME FROM STOCK_TYPE");
            rs = stmt.executeQuery();
            while (rs.next()) {
                //lst.add(rs.getString(1));
                List<List<Object>> lst1 = getDecline(rs.getString(1));
                if (!lst1.isEmpty() && lst1.size() > 0) {
                    for (int i = 0; i < lst1.size(); i++) {
                        System.out.println(rs.getString(1) + " " + lst1.get(i));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 7. 关闭资源（反向顺序）
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 根据板块获取跌幅最大的两个股票
     * */
    public static List<List<Object>> getDecline(String type_name) {
        List<List<Object>> lst = new ArrayList<>();
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            conn = MyUtil.getConnection();
            stmt = conn.prepareStatement("SELECT name FROM TYPE_STOCK WHERE TYPE_NAME = ?");
            stmt.setString(1, type_name);
            rs = stmt.executeQuery();
            while (rs.next()) {
                if (!rs.getString(1).contains("ST")) {
                    Double vs = getStockDecline(rs.getString(1));
                    if (vs > 0D) {
                        List<Object> lst1 = new ArrayList<>();
                        lst1.add(rs.getString(1));
                        lst1.add(vs);
                        lst.add(lst1);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 7. 关闭资源（反向顺序）
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return lst.stream().sorted((a, b) -> {
            if ((double)a.get(1) - (double)b.get(1) > 0)
                return -1;
            return 1;
        }).limit(3).collect(Collectors.toList());
    }

    public static Double getStockDecline(String name) {
        Double vs = 0D;
        List<Double> lst = new ArrayList<>();
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            conn = MyUtil.getConnection();
            stmt = conn.prepareStatement("SELECT m.PRICE FROM DF_STOCK_DATE m WHERE m.NAME = ? ORDER BY m.STOCK_DATE DESC LIMIT 0, 7");
            stmt.setString(1, name);
            rs = stmt.executeQuery();

            while (rs.next()) {
                lst.add(rs.getDouble(1));
            }
            if (!lst.isEmpty() && lst.size() > 1 && lst.get(0) < lst.get(lst.size() - 1)) {
                vs = getVs(lst);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 7. 关闭资源（反向顺序）
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return vs;
    }

    public static Double getVs(List<Double> lst) {
        Item max = new Item();
        Item min = new Item();

        min.value = lst.get(0);
        min.index = 0;

        for (int i = 0; i < lst.size(); i++) {
            Double n = lst.get(i);
            if (max.value < n) {
                max.value = n;
                max.index = i;
            }

            if (min.value > n) {
                min.value = n;
                min.index = i;
            }
        }

        if (lst.get(0) > lst.get(lst.size() - 1)) {
            return 0D;
        }

        if (max.index < min.index) {
            return 0D;
        }

        return max.value / min.value;
    }
}













































